package cn.com.bluemoon.daps.model.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.common.KeyAnnotationClz;
import cn.com.bluemoon.common.UserPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.domp.api.FastDFSService;
import cn.com.bluemoon.daps.model.service.DapDataModelExportService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 模型导出 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-02-02
 */
@Api(tags = "模型导出")
@RestController
@Slf4j
@RequestMapping("/modelExport")
@BmPermission(value = "模型导出功能")
@Validated
public class DapDataModelExportController {
    @Resource
    private DapDataModelExportService exportService;

    @GetMapping("/normal")
    @ApiOperation(value = "根据模型id导出模型表信息，接口直接返回文件")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Void.class)})
    @BmPermission(value = "model:excelTable:export", name = "导出模型", queryTokenPerm = true)
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行导出功能")
    public void exportExcel(HttpServletResponse response,
                            @ApiParam("模型id") @RequestParam String modelId) {
        exportService.exportExcelByModelId(response, modelId);
    }


    @GetMapping("/")
    @ApiOperation(value = "根据模型id导出模型表信息，接口返回文件系统url，提供前端下载")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = FastDFSService.UploadResult.class)})
    @BmPermission(value = "model:excelTable:export", name = "导出模型", queryTokenPerm = true)
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行导出功能")
    public ResultBean<?> exportExcel(@ApiParam("模型id") @RequestParam String modelId) {
        return exportService.exportExcelByModelId(modelId);
    }


    @GetMapping("/v2")
    @ApiOperation("模型导出excel")
    @BmPermission(value = "model:table:import", name = "模型导出excel", queryTokenPerm = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Void.class)})
    @UserPermission.SingleRow(keyFormClz = KeyAnnotationClz.RequestParam, key = "modelId", errorMsg = "只允许模型授权者进行下载模板功能")
    public void exportV2(@ApiParam("模型id") @RequestParam("modelId") String modelId,
                         @ApiParam("分组id") @RequestParam(value = "groupId", required = false) String groupId,
                         HttpServletResponse response) {
        exportService.exportModelV2ByModelId(response, modelId, groupId);
    }

}
